package com.fengpanlong.service;

import java.util.ArrayList;
import java.util.List;

import com.fengpanlong.dao.EmployeeDao;
import com.fengpanlong.dao.IdeaCommentDao;
import com.fengpanlong.dao.IdeaDao;
import com.fengpanlong.entity.Employee;
import com.fengpanlong.entity.Idea;
import com.fengpanlong.entity.IdeaComment;
import com.fengpanlong.util.IdeaHelp;
import com.fengpanlong.util.IdeaHelp.IdeaCommentHelp;

public class IdeaService {
	/**
	 * 保存想法
	 * @param idea
	 */
	public static void save(Idea idea) {
		IdeaDao id = new IdeaDao();
		id.save(idea);
	}
	/**
	 * 通过想法id查找ideaHelp对象
	 * @param ideaId
	 * @return
	 */
	public static IdeaHelp getIdeaHelpByIdeaId(String ideaId) {
		IdeaHelp ideaHelp = new IdeaHelp();
		IdeaDao id = new IdeaDao();
		Idea idea = id.findById(ideaId);
		if(idea == null) {
			return null;
		}
		ideaHelp.setIdea(idea);
		ideaHelp.setIdeaId(idea.getId());
		EmployeeDao ed = new EmployeeDao();
		Employee employee= ed.findByIdeaId(ideaId);
		ideaHelp.setEmployeeName(employee.getUsername());
		
		IdeaCommentDao icd = new IdeaCommentDao();
		List<IdeaComment> ideaCommentList = icd.findIdeaCommentsByIdeaId(ideaId);
		List<IdeaCommentHelp> ideaCommentHelpList = new ArrayList<IdeaHelp.IdeaCommentHelp>();
		for(IdeaComment ideaComment : ideaCommentList) {
			IdeaCommentHelp ideaCommentHelp = ideaHelp.new IdeaCommentHelp();
			employee = ed.findByIdeaCommentId(ideaComment.getId());
			ideaCommentHelp.setEmployeeName(employee.getUsername());
			ideaCommentHelp.setIdeaComment(ideaComment);
			ideaCommentHelpList.add(ideaCommentHelp);
		}
		ideaHelp.setIdeaCommentHelpList(ideaCommentHelpList);
		return ideaHelp;
	}
	/**
	 * 通过想法评论id删除想法评论
	 * @param request
	 * @param response
	 */
	public static void delIdeaCommentByIdeaCommentId(String ideaCommentId) {
		IdeaCommentDao icd = new IdeaCommentDao();
		icd.delById(ideaCommentId);
	}
	/**
	 * 保存评论
	 * @param ideaComment
	 */
	public static void saveIdeaComment(IdeaComment ideaComment) {
		IdeaCommentDao icd = new IdeaCommentDao();
		icd.save(ideaComment);
	}
	/**
	 * 通过想法id删除想法
	 * @param ideaId
	 */
	public static void delIdeaByIdeaId(String ideaId) {
		IdeaCommentDao icd = new IdeaCommentDao();
		icd.delIdeaCommentsByIdeaId(ideaId);
		IdeaDao id = new IdeaDao();
		id.delById(ideaId);
	}
	/**
	 * 通过想法id查找想法
	 * @param ideaId
	 * @return
	 */
	public static Idea findIdeaByIdeaId(String ideaId) {
		IdeaDao id = new IdeaDao();
		return id.findById(ideaId);
	}
	/**
	 * 通过评论id查找想法
	 * @param ideaCommentId
	 * @return
	 */
	public static Idea findIdeaByIdeaCommentId(String ideaCommentId) {
		IdeaDao id = new IdeaDao();
		return id.findByIdeaCommentId(ideaCommentId);
	}
	/**
	 * 更新想法
	 * @param idea
	 */
	public static void update(Idea idea) {
		IdeaDao id = new IdeaDao();
		id.update(idea);
	}
	
}
